package com.itboye.ihomebank.activity.finance;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.MyWalletBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.presenter.JinRongPresenter;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;
import com.itboye.ihomebank.util.WritePassPopupWindow;

import java.util.Observable;
import java.util.Observer;

/**
 * 转入金额
 */
public class ZhuanRuActivity extends BaseOtherActivity implements Observer {

    private LinearLayout linearTurOutInto;
    private ImageView close_icon;
    private JinRongPresenter presenter;
    private EditText edittextZhuanru;
    private String uid;
    private UserPresenter userPresenter;
    private TextView tvPriceSurplus;
    private Double aDouble;//转换
    private TextView add_shap_title_tv;
    private String id;
    MyWalletBean myWalletBean;

    private WritePassPopupWindow passPopupWindow;
    EditText pass;

    @Override
    protected int layout_id() {
        return R.layout.activity_turn_out;
    }

    @Override
    protected void onInitController() {

    }

    @Override
    protected void onInit() {
        add_shap_title_tv.setText("租金宝转入");
        presenter = new JinRongPresenter(this);
        userPresenter = new UserPresenter(this);
        id = getIntent().getStringExtra("id");
        onTextWarcher();
        uid = (String) SPUtils.get(this, null, SPContants.USER_ID, "");
        showProgressDialog("数据加载中...", true);
        userPresenter.myWallet(uid);

        IntentFilter filter01 = new IntentFilter(FinanceActivity.DAOQI);
        registerReceiver(jieshuReceiver, filter01);

    }
    BroadcastReceiver jieshuReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            finish();
            ByAlert.alert("该商品不在购买时间内");
        };
    };
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(jieshuReceiver);
    }

    public void onTextWarcher() {
        edittextZhuanru.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (edittextZhuanru.getText().toString().length() > 0) {
                    linearTurOutInto.setClickable(true);
                    Log.d("edittextZhuanru", s + "");
                } else {
                    Log.d("edittextZhuanrus", s + "");
                    linearTurOutInto.setClickable(false);
                }
            }
        });
    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.linearTurOutInto:
                if (edittextZhuanru.getText().toString().equals("") || edittextZhuanru.getText().toString() == null) {
                    ByAlert.alert("请输入购买金额");
                } else {
                    aDouble = Double.parseDouble(edittextZhuanru.getText().toString());
                    if (aDouble > Double.parseDouble(myWalletBean
                            .getBalance()+"") / 100) {
                        ByAlert.alert("余额不足");
                        return;
                    }
                    Log.d("aDouble", id + "\n" + uid);

                    passPopupWindow = new WritePassPopupWindow(this, this);
                    pass = (EditText) passPopupWindow.getContentView()
                            .findViewById(R.id.pass);
                    passPopupWindow.setBackgroundDrawable(new BitmapDrawable());
                    passPopupWindow.setFocusable(true);
                    passPopupWindow.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
                    passPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
                    passPopupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);
                }
                break;
            case R.id.close_icon:
                this.finish();
                break;
            case R.id.btn_ok:
                String password = pass.getText().toString();
                if (password.equals("")) {
                    ByAlert.alert("请输入余额支付密码");
                    return;
                }
                userPresenter.yueYanzheng(uid, password);
                passPopupWindow.dismiss();
                break;
            case R.id.btn_cancel:
                passPopupWindow.dismiss();
                break;
        }

    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            if (entity.getEventType() == JinRongPresenter.zhuan_ru_success) {
                ByAlert.alert(entity.getData() + "");
                userPresenter.myWallet(uid);//购买成功后刷新当前金额
                Intent intent = new Intent("zhuangzhang_ok");
                sendBroadcast(intent);
                finish();
            }
            if (entity.getEventType() == JinRongPresenter.zhuan_ru_fail) {
                ByAlert.alert(entity.getData() + "");
            }
            if (entity.getEventType() == UserPresenter.wallet_success) {
                myWalletBean = (MyWalletBean) entity.getData();
                tvPriceSurplus.setText("可用余额" + Double.parseDouble(myWalletBean
                        .getBalance()+"") / 100 + "元");

            }
            if (entity.getEventType() == UserPresenter.wallet_fail) {
                ByAlert.alert(entity.getData().toString());
            }
            if (entity.getEventType() == UserPresenter.yzyue_success) {
                showProgressDialog("购买中...", true);
                presenter.onZhuanRu(uid, id, aDouble * 100 + "");
            }
            if (entity.getEventType() == UserPresenter.yzyue_fail) {
                ByAlert.alert(entity.getData());
            }
        }
        try {
            closeProgressDialog();
        } catch (Exception e) {
        }
    }
}
